package com.example.cognitivesystemserver.controller;

import com.example.cognitivesystemserver.Exceptions.ParamsException;
import com.example.cognitivesystemserver.Model.UserModel;
import com.example.cognitivesystemserver.base.ResultInfo;
import com.example.cognitivesystemserver.base.ResultType;
import com.example.cognitivesystemserver.pojo.Login;
import com.example.cognitivesystemserver.service.LoginService;
import com.example.cognitivesystemserver.utils.Base64Util;
import com.example.cognitivesystemserver.utils.CookieUtil;
import com.example.cognitivesystemserver.utils.PassToken;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
@RestController
@CrossOrigin
public class LoginController {

    /**
     * 用户注册
     * @param name//姓名
     * @param userId//学号、工号
     * @param userPwd
     * @param role//用户角色 1：管理员 2：教师 3：学生
     */

    /**
     * 用户登录
     * @param role
     * @param userId
     * @param password
     */
    @Resource
    private LoginService loginService;

    //注册
    @PostMapping("/login/register")
    @ResponseBody
    public ResultInfo register(String name, String userId,String userPwd,String role){

        log.info("name:{}",name);
        log.info("Id:{}",userId);
        log.info("password:{}",userPwd);

        ResultInfo data=new ResultInfo();
        try {
            UserModel userModel=loginService.register(name,userId,userPwd,role);
            data.setData(userModel);
            data.setMsg("注册成功");
        }catch (ParamsException p){
            data.setCode(p.getCode());
            data.setMsg(p.getMsg());
        }

        return data;

    }


    //登录
    @PostMapping("/login/LoginAcc")
    @PassToken
    public Object Login(@RequestBody Login login){

        String userId=login.getUserId();
        String role=login.getRole();
        String password=login.getPassword();
        ResultInfo data=new ResultInfo();

        //通过try catch捕获service层的异常
        try{
            //调用Service层登录方法
            UserModel userModel = loginService.login(userId,password,role);

            //设置ResultInfo的result，登录成功的信息，将数据返回给请求
            data.setData(userModel);
            //判断密码是否正确
            if (null==userModel){
                data.setCode(200);
                data.setMsg("密码错误！");
            }
        }catch (ParamsException p){
            //失败状态码
            data.setCode(p.getCode());
            //失败信息
            data.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            //自定义异常
            data.setCode(500);
            data.setMsg("登录失败！");
        }

        return data;
    }


    //更改登录密码
    @PostMapping("user/updatePwd")
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                         String oldPassword,String newPassword,String repeatPassword){
        ResultInfo resultInfo=new ResultInfo();
        try {
            //得到cookie中的学生id
            String userId;
            String userIdString= CookieUtil.getCookieValue(request,"userIdStr");
            if(StringUtils.isBlank(userIdString)){
                userId=null;
            }
            userId= Base64Util.decoderUserID(userIdString);
            loginService.updatePassWord(userId,oldPassword,newPassword,repeatPassword);
        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("修改密码失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    @RequestMapping("main")
    public String main(HttpServletRequest request){
        String userId;
        //获取cookie中的id
        String userIdString= CookieUtil.getCookieValue(request,"userIdStr");
        if(StringUtils.isBlank(userIdString)){
            userId=null;
        }
        userId= Base64Util.decoderUserID(userIdString);
        //设置session
        List<Login> login=loginService.selectByUserId(userId);
        request.getSession().setAttribute("user",login);

        return "main";

    }
    @GetMapping("/user/getLogin")
    public Object getLogin(String userId){
        List<Login> login = loginService.selectByUserId(userId);
        return new ResultType(0, "", login);
    }
}
